package com.itheima.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.db.UserThaw;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.LogListVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.UserService;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    /**
     * 用户列表数据
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/manage/users")
    public PageBeanVo findUsers(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNumber,
            @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize
    ) {
        return userManager.findUsers(pageNumber, pageSize);
    }


    /**
     * 用户详情
     *
     * @return
     */
    @GetMapping("/manage/users/{userId}")
    public UserInfo findUserById(
            @PathVariable("userId") Long userId
    ) {
        return userManager.findUserById(userId);
    }


    /**
     * 视频列表
     *
     * @return
     */
    @GetMapping("/manage/videos")
    public PageBeanVo findVideoByUserId(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNumber,
            @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize,
            Long uid
    ) {
        return userManager.findVideoByUserId(pageNumber, pageSize, uid);
    }


    /**
     * 动态列表
     *
     * @return
     */
    @GetMapping("/manage/messages")
    public PageBeanVo findMovementList(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNumber,
            @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize,
            Long uid,
            String state
    ) {
        Integer stateInt = null;
        //前端有坑 没有传递state的时候 传入了 ''空串  state 需要判断 这是前端的小bug
        if (StrUtil.isNotEmpty(state)) {
            stateInt = Integer.valueOf(state);//转换成int
        }

        return userManager.findMovementList(pageNumber, pageSize, uid, stateInt);
    }


    //动态详情
    @GetMapping("/manage/messages/{movementId}")
    public MovementVo findMovementList(

            @PathVariable String movementId
    ) {
        return userManager.findMovementById(movementId);
    }

    //评论列表
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findCommentList(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNumber,
            @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize,
            @RequestParam(name = "messageID") String movementId
    ) {
        return userManager.findCommentList(movementId, pageNumber, pageSize);
    }

    /**
     * 后台动态审核通过
     *
     * @param list 前端传过来的动态id的集合
     * @return
     */
    @PostMapping("/manage/messages/pass")
    public Map<String, String> messagesPass(@RequestBody List<String> list) {
        //判断传过来的集合是否为空
        if (CollectionUtil.isNotEmpty(list)) {
            //调用manager
            return userManager.messagesPass(list);
        }
        return null;
    }

    /**
     * 后台的动态审核  不通过
     *
     * @param list
     * @return
     */
    @PostMapping("/manage/messages/reject")
    public Map<String, String> massageReject(@RequestBody List<String> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            return userManager.massageReject(list);
        }
        return null;
    }

    /**
     * 用户冻结操作
     */
    @PostMapping("/manage/users/freeze")
    public Map freezeOperation(@RequestBody UserFreeze userFreeze) {
        System.err.println(userFreeze);
        Boolean flag = userManager.freezeOperation(userFreeze);
        if (flag) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("message", "操作成功");
            return resultMap;
        }
        return null;
    }

    /**
     * 用户解冻操作
     */
    @PostMapping("/manage/users/unfreeze")
    public Map freezeUnseal(@RequestBody UserThaw userThaw/*Map<String, String> map*/) {
//        Long userId = Long.valueOf(map.get("userId"));
//        String reasonsForThawing = String.valueOf(map.get("reasonsForThawing"));
//        UserThaw userThaw = new UserThaw();
//        userThaw.setUserId(userId);
//        userThaw.setReasonsForThawing(reasonsForThawing);
        System.err.println(userThaw);
        Boolean flag = userManager.freezeUnseal(userThaw);
        if (flag) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("message", "操作成功");
            return resultMap;
        }
        return null;
    }

    /**
     * 分页查询用户日志
     */
    @GetMapping("/manage/users/findByLog")
    public PageBeanVo findByLog(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNumber,
            @RequestParam(defaultValue = "10", name = "pagesize") Integer pageSize,
            String startDate, String endDate,
            @RequestParam(defaultValue = "") String nickName
    ) {
        return userManager.findByLog(pageNumber, pageSize, startDate, endDate, nickName);
    }

    /**
     * 查询用户地理位置信息
     */
    @GetMapping("/manage/users/userlocation/{userId}")
    public Map<String, String> findUserLocationById(@PathVariable("userId") Long userId) {
        return userManager.findUserLocation(userId);
    }


}
